#include "organization.hpp"
#include "db/sqlite_conn.hpp"





namespace bz
{

	REFLECTION(organization, id, pid, name, create_time, sort)

	
	
	organization::organization()
	{
		
	}

	organization::~organization()
	{
	}

	void organization::init() 
	{
		ormpp_auto_key auto_key{
		"id"
		};
		auto db = sqlite_conn::get_conn();
		sqlite_guard guard(db);
		db->set_auto_key<organization>(auto_key);
	}

	std::string organization::index()
	{
		auto db = sqlite_conn::get_conn();
		sqlite_guard guard(db);
		auto rows =db->query<organization>();
		json lists = json::array();
		for (auto it : rows)
		{
			json row;
			to_json(row, it);
			lists.push_back(row);
		}
		return lists.dump();
	}

	int organization::add()
	{
		auto db = sqlite_conn::get_conn();
		sqlite_guard guard(db);
		return db->insert(*this);
	}

	bool organization::remove() 
	{
		auto db = sqlite_conn::get_conn();
		sqlite_guard guard(db);
		auto res = db->query(FID(organization::pid), "=", id);
		if (res.size() > 0) {
			return false;
		}
		return db->delete_records(FID(organization::id), "=", id);
	}

	int organization::update() 
	{
		auto db = sqlite_conn::get_conn();
		sqlite_guard guard(db);
		return db->update(*this);
	}

	
}